package com.wly.packet;

import java.lang.reflect.Method;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wly.define.PacketDefine;

public class PacketFactory
{

	private static PacketFactory _instance = null;
	private static HashMap<Integer, String> classInfo;

	static private Logger log = LoggerFactory.getLogger("PacketFactory");
	
	private PacketFactory()
	{
		initClassInfo();
	}
	
	static public PacketFactory getInstance()
	{
		if(_instance == null)
		{
			_instance = new PacketFactory();
		}
		System.out.println("getInstance"+_instance.toString());
		return _instance;
	}
	
	private void initClassInfo()
	{
		System.out.println("initClassInfo");
		classInfo = new  HashMap<Integer, String>();
		classInfo.put(PacketDefine.WLY_PCKET_LOGIN, "com.wly.packet.CSLoginPacket");
		classInfo.put(3, "com.wly.packet.TestPacket");
	}
	
	public Packet decodeObject(int type, byte[] byteArr)
	{
		try
		{
		Packet pkg;
		System.out.println("packet type:" + type);
		String className = (String)classInfo.get(type);
		if(className == null)
		{
			System.out.println("unKown packet type:" + type);
			log.info("unKown packet type:" + type);
			return null;
		}
		System.out.println("get packet define:" + className);
		Class cls = Class.forName(className);
		pkg = (Packet)cls.newInstance();
		pkg.unSerialization(byteArr);
		return pkg;
		}
		catch (Exception e)
		{
			System.out.println("Exception:");
			e.printStackTrace();
			return null;
		}
	}
}
